<?php

namespace App\Custom\Wechat;

use EasyWeChat\Factory;

trait Main
{


    /**
     * 获取下单支付主体
     * @return \EasyWeChat\Payment\Application
     */
    public function getPaymentApp()
    {
        $config = get_setting('applet');
        return Factory::payment([
            'order_prefix' => $config['order_prefix'],
            'refund_prefix' => $config['refund_prefix'],
            'sandbox' => false,
            'app_id' => $config['app_id'],
            'mch_id' => $config['mch_id'],
            'key' => $config['app_key'],
            'notify_url' => $config['notify_url'],
            'key_path' => storage_path('/cert/key.pem'),
            'cert_path' => storage_path('/cert/cert.pem'),
        ]);
    }

    /**
     * 获取小程序主体
     * @return \EasyWeChat\MiniProgram\Application
     */
    public function getWechatApp()
    {
        $config = get_setting('applet');
        return Factory::miniProgram([
            'app_id' => $config['app_id'],
            'secret' => $config['app_secret']
        ]);
    }


    /**
     * 返回小程序配置
     * @return mixed
     */
    public function  getMiniProgramEasyWechat()
    {
        // 获取服务器配置
        $mqConfig = get_setting('applet');
        $config = [
            'app_id'  => $mqConfig['app_id'],
            'secret'  => $mqConfig['app_secret']
        ];
        return Factory::miniProgram($config);
    }


    /**
     * 微信支付
     * @return \EasyWeChat\Payment\Application
     */
    public function getPaymentWechat()
    {
        // 获取服务器配置
        $mqConfig = get_setting('applet');
        return Factory::payment([
            'app_id'     => $mqConfig['app_id'],
            'mch_id'    => $mqConfig['mch_id'],
            'key'       => $mqConfig['app_key']
        ]);
    }
}
